﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace atmsystem_ss
{
    public partial class frmInformation_Customer : Form
    {

        private SqlConnection con = new SqlConnection();
        private DataTable dt = new DataTable("tblCustomer");
        private SqlDataAdapter da = new SqlDataAdapter();
        
        private void connect()
            
        {
            string cn = "Server=.\\SQLEXPRESS;Database=atmsystemsangson;User Id=sa;Password=123456";
            try
            {
                con = new SqlConnection(cn);
                con.Open();//mo ket noi

            }
            catch (Exception ex)
            {
                MessageBox.Show("Khong ket noi duoc co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }

        public frmInformation_Customer()
        {
            InitializeComponent();
        }

        private void frmCreate_Customer_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
           
            binding();
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand();// khai bao 1 command
            command.Connection = con; //ket noi
            command.CommandType = CommandType.Text;//khai bao kieu command
            command.CommandText = @"Select  fldID_Customer as N'CustomerID',
                                            fldFull_Name as N'FullName',
                                            fldID_Number as N'IDNumber',
                                            (Case fldSex when 'True' then N'Male' else N'Female' end) as N'Sex',
                                            fldBirth as N'Birth',
                                            fldAddress as N'Address',
                                            fldPhone as N'Phone',
                                            fldEmail as N'Email'
                                            From tblCustomer";
            da.SelectCommand = command;//gan commad cho da
            da.Fill(dt);//nap du lieu cho table
            grvdata.DataSource = dt;//load du lieu len datagridview
                
        }
        private void binding()
        { 
            txtID_Customer.DataBindings.Clear();
            txtID_Customer.DataBindings.Add("Text", grvdata.DataSource, "CustomerID");
            txtFull_Name.DataBindings.Clear();
            txtFull_Name.DataBindings.Add("Text", grvdata.DataSource, "FullName");
            txtID_Number.DataBindings.Clear();
            txtID_Number.DataBindings.Add("Text", grvdata.DataSource, "IDNumber");
            cboSex.DataBindings.Clear();
            cboSex.DataBindings.Add("Text", grvdata.DataSource, "Sex");
            txtBirth.DataBindings.Clear();
            txtBirth.DataBindings.Add("Text", grvdata.DataSource, "Birth");
            txtAddress.DataBindings.Clear();
            txtAddress.DataBindings.Add("Text", grvdata.DataSource, "Address");
            txtPhone.DataBindings.Clear();
            txtPhone.DataBindings.Add("Text", grvdata.DataSource, "Phone");
            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", grvdata.DataSource, "Email");

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Ban co muon thoat khong ?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Close();
                Dispose();
                disconnect();
                frmAdmin _frmAdmin = new frmAdmin();
                _frmAdmin.Show();
            }
           
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmCreate_Customer _frmCreate_Customer = new frmCreate_Customer();
            _frmCreate_Customer.Show();
            Hide();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("CustomerID = " + Convert.ToString(txtID_Customer.Text))[0];
            row.BeginEdit();
            row["FullName"] = txtFull_Name.Text;
            row["IDNumber"] = txtID_Number.Text;
            if (cboSex.Text == "Male")
                row["Sex"] = true;
            else if (cboSex.Text == "Female")
                row["Sex"] = false;
            row["Birth"] = txtBirth.Text;
            row["Address"] = txtAddress.Text;
            row["Phone"] = txtPhone.Text;
            row["Email"] = txtEmail.Text;
            row.EndEdit();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = con;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"update tblCustomer set 
                                                                fldFull_Name = @fldFull_Name,
                                                                fldID_Number= @fldID_Number,
                                                                fldSex = @fldSex,
                                                                fldBirth= @fldBirth,
                                                                fldAddress= @fldAddress,
                                                                fldPhone= @fldPhone,
                                                                fldEmail= @fldEmail
                                                                where fldID_Customer= @fldID_Customer";
            commandUpdate.Parameters.Add("@fldID_Customer", SqlDbType.NVarChar, 50, "CustomerID");
                
            commandUpdate.Parameters.Add("@fldFull_Name", SqlDbType.NVarChar, 50, "FullName");

            commandUpdate.Parameters.Add("@fldID_Number", SqlDbType.NVarChar, 50, "IDNumber");

            commandUpdate.Parameters.Add("@fldSex", SqlDbType.Bit, 50, "Sex");
            commandUpdate.Parameters.Add("@fldBirth", SqlDbType.DateTime, 50, "Birth");
            commandUpdate.Parameters.Add("@fldAddress", SqlDbType.NVarChar, 50, "Address");

            commandUpdate.Parameters.Add("@fldPhone", SqlDbType.NVarChar, 50, "Phone");

            commandUpdate.Parameters.Add("@fldEmail", SqlDbType.NVarChar, 50, "Email");
            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            MessageBox.Show("Ban da Update thanh cong", "Update successed", MessageBoxButtons.OK, MessageBoxIcon.Information);

           
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("CustomerID = " + Convert.ToString(txtID_Customer.Text))[0];
            row.BeginEdit();
            row.Delete();
            row.EndEdit();
            SqlCommand commandDelete = new SqlCommand();
            commandDelete.Connection = con;
            commandDelete.CommandType = CommandType.Text;
            commandDelete.CommandText = @"delete from tblCustomer where fldID_Customer = @fldID_Customer";
            commandDelete.Parameters.Add("@fldID_Customer", SqlDbType.NVarChar, 50, "CustomerID");
            da.DeleteCommand = commandDelete;
            da.Update(dt);
            MessageBox.Show("Ban da Delete thanh cong", "Delete successed", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

       
       

     
     
    }
}
